import * as PIXI from 'pixi.js'
import { PlayerController } from "./playerController";
import { PlayerProps, playerOrderType } from '@/Util/Constant';
import { CardList } from './cardList';
import { CardType } from '@/share/commonType';
import { UserAsset } from '../userAsset';
import Hlddz from '../model/hlddz';
import { getScaleSize, loadRemoteResource } from '@/Util';
import { Timer } from '../button/timer';
let TextureCache = PIXI.utils.TextureCache

export class Player {
    // player总共控制四个元素 人物 名字（头像） 倒计时 出牌
    private isLeft: boolean;
    private baseContainer: PIXI.Container;
    private playerSprite: PIXI.Sprite;
    private prepareIcon: PIXI.Sprite;
    private container: PIXI.Container = new PIXI.Container();
    private cardNumText: PIXI.Text;
    private cardList: CardList;
    private userAsset: UserAsset;
    private scaleSize: number = getScaleSize();
    private timer: Timer;
    constructor(props: PlayerProps) {
        this.isLeft = props.playerOrder == playerOrderType.Before
        this.baseContainer = props.baseContainer
        this.playerSprite = props.playerSprite
        this.container.x = props.posX
        this.container.y = props.posY
        this.baseContainer.addChild(this.container)
        this.cardList = new CardList(this.baseContainer)
        this.load()
        this.createTimer()
    }

    createTimer() {
        this.timer = new Timer({
            baseContainer: this.container,
            scaleSize: this.scaleSize,
            posX: this.isLeft ? 450 * this.scaleSize : - 160 * this.scaleSize,
            posY: 100 * this.scaleSize
        })
    }

    showTimer() {
        this.cardList.clearCard()
        this.timer.startInterVal()
    }

    hiddenTimer() {
        this.timer.endInterVal()
    }

    load() {
        this.renderPlayer()
        this.loadPrepareIcon()
        this.userAsset = new UserAsset({
            baseContainer: this.container,
            posX: this.isLeft ? 0 : 30 * this.scaleSize,
            posY: this.isLeft ? 200 * this.scaleSize : 300 * this.scaleSize,
            scale: this.scaleSize * this.scaleSize
        })
    }

    renderCard(cardList: CardType[]) {
        let posX: number;
        if (cardList) {
            let cardLength = cardList.length;
            posX = this.isLeft ? 450 * this.scaleSize : this.container.x - ((cardLength - 1) * 38 + 135) * this.scaleSize
        } else {
            posX = this.isLeft ? 450 * this.scaleSize : this.container.x - 160 * this.scaleSize
        }
        let posY = this.container.y + 100 * this.scaleSize
        this.cardList.changeCard({ cardList, posX, posY, scaleSize: this.scaleSize })
    }

    loadPrepareIcon() {
        this.prepareIcon = new PIXI.Sprite(TextureCache['ok'])
        this.prepareIcon.visible = false
        this.prepareIcon.scale.set(this.scaleSize)
        this.container.addChild(this.prepareIcon)
    }

    showPrepareIcon(status: boolean) {
        this.prepareIcon.visible = status
        if (status) {
            this.prepareIcon.x = this.isLeft ? this.playerSprite.x + 200 * this.scaleSize : - 100 * this.scaleSize
            this.prepareIcon.y = 100 * this.scaleSize
        }
    }

    renderPlayer() {
        this.playerSprite.x = this.isLeft ? 200 * this.scaleSize : 0
        this.playerSprite.scale.set(getScaleSize())
        this.container.addChild(this.playerSprite)
    }

    renderLandlord() {
        let landSprite = new PIXI.Sprite(TextureCache['land'])
        landSprite.scale.set(0.7 * this.scaleSize)
        landSprite.x = this.isLeft ? 0 : 160 * this.scaleSize
        landSprite.y = 0
        this.container.addChild(landSprite)
    }
    setCardNumber(cardNum: string) {  // 渲染剩余牌数
        let text = '剩余: ' + cardNum + '张'
        this.cardNumText.text = text
    }
    renderCardNum() {
        this.cardNumText = new PIXI.Text('', {
            fontFamily: "黑体",
            fill: 'yellow',
            fontSize: 25,
            align: "center",
            dropShadow: true,
            dropShadowColor: "orange",
            dropShadowBlur: 4,
            dropShadowAngle: Math.PI / 2,
            dropShadowDistance: 2,
        }) as PIXI.Text;
        this.cardNumText.x = this.isLeft ? 0 : 180 * this.scaleSize
        this.cardNumText.y = 100 * this.scaleSize
        this.cardNumText.scale.set(this.scaleSize)
        this.container.addChild(this.cardNumText)
    }

    setHldNum(num: number) {
        this.userAsset.setHldNum(num)
    }

    setPlayer(name: string, model: Hlddz) {
        if (name) {
            this.playerSprite.visible = true
            let playerinfo = model.getPlayerInfo(name)
            loadRemoteResource(playerinfo.avatar).then((texture) => {
                let sprite = new PIXI.Sprite(texture)
                this.userAsset.setUser(playerinfo.nickname, sprite)
            })
            this.userAsset.setHldNum(playerinfo.hld)
        } else {
            this.userAsset.hidden()
            this.playerSprite.visible = false
        }
    }

    gameRestart() {
        // 清除元素
        this.container.removeChildren()
        this.cardList.clearCard()
        this.load()
        this.createTimer()
    }
}